//
//  XieYiView.m
//  AI
//
//  Created by WTTT on 2023/7/25.
//

#import "XieYiView.h"

@implementation XieYiView

- (void)SQ_UIViewForSelfView{
    self.backgroundColor = UIColor.clearColor;
    UIView * bgimage = [[UIView alloc]init];
    bgimage.backgroundColor =  SQColorF1F1F1;
    bgimage.layer.cornerRadius = 12;
    bgimage.layer.masksToBounds = YES;
    bgimage.clipsToBounds = YES;
    [self addSubview:bgimage];
    [bgimage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(TopHeight+40);
        make.width.mas_equalTo(SCREEN_WIDETH - 60);
        make.centerX.equalTo(self);
        make.height.mas_equalTo(SCREEN_HEIGHT-210-TopHeight - FootHeight);
    }];
    
    [self addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(bgimage).insets(UIEdgeInsetsMake(20, 20, 50, 20));
    }];
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:YHXY]]];
    [bgimage addSubview:self.yonghuButton];
    [bgimage addSubview:self.yinsiButton];
    [self.yonghuButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDETH/2.f-30);
        make.height.mas_equalTo(44);
        make.left.equalTo(bgimage);
        make.bottom.equalTo(bgimage.mas_bottom).offset(0);
    }];
    
    [self.yinsiButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDETH/2.f-30);
        make.height.mas_equalTo(44);
        make.right.equalTo(bgimage.mas_right);
        make.bottom.equalTo(bgimage.mas_bottom).offset(0);
    }];
    
    _leftButton = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(20, 20) midmargin:0];
    _leftButton.image = SQGetImage(@"退出图标");
    [self addSubview:self.leftButton];
    [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(60);
            make.centerX.equalTo(self);
            make.top.equalTo(self.yonghuButton.mas_bottom).offset(20);
    }];
    

}
-(void)SQ_changeIndex:(UIButton*)segm{
 
    if (segm == self.yonghuButton.btn) {
        _yonghuButton.backgroundColor = SQColor(@"#E62DFD");
        _yinsiButton.backgroundColor = SQColor(@"#CCCCCC");
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:YHXY]]];
        [self.webView reload];
    } else {
        [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:YSTK ]]];
        [self.webView reload];
        _yinsiButton.backgroundColor = SQColor(@"#E62DFD");
        _yonghuButton.backgroundColor = SQColor(@"#CCCCCC");
    }
}

- (HeaderView *)yonghuButton{
    if (!_yonghuButton) {
        _yonghuButton = [[HeaderView alloc]init];
        _yonghuButton.text = SQNSLocalString(@"用户协议");
        _yonghuButton.colorString = @"#FFFFFF";
        _yonghuButton.backgroundColor = SQColor(@"#E62DFD");
        
        _yonghuButton.font = FONT_REGULAR(14);
        [_yonghuButton.btn addTarget:self action:@selector(SQ_changeIndex:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _yonghuButton;
}
- (HeaderView *)yinsiButton{
    if (!_yinsiButton) {
        _yinsiButton = [[HeaderView alloc]init];
        _yinsiButton.text = SQNSLocalString(@"隐私协议");
        _yinsiButton.colorString = @"#FFFFFF";
        _yinsiButton.font = FONT_REGULAR(14);
         
        _yinsiButton.backgroundColor = SQColor(@"#CCCCCC");
        [_yinsiButton.btn addTarget:self action:@selector(SQ_changeIndex:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _yinsiButton;
}

-(WKWebView *)webView{
    if (_webView == nil) {
        NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
               
               WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
               WKUserContentController *wkUController = [[WKUserContentController alloc] init];
               [wkUController addUserScript:wkUScript];
               
               WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
               wkWebConfig.userContentController = wkUController;
               
               // 创建设置对象
               WKPreferences *preference = [[WKPreferences alloc]init];
               // 设置字体大小(最小的字体大小)
               preference.minimumFontSize = 15;
               // 设置偏好设置对象
               wkWebConfig.preferences = preference;
               WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:wkWebConfig];
               webView.scrollView.alwaysBounceVertical = NO;
        webView.backgroundColor = UIColor.clearColor;
        webView.scrollView.backgroundColor = UIColor.clearColor;
        webView.opaque = NO;
        _webView = webView;
    }
    return _webView;
}
@end
